<?php 
//连接数据库
function connect($arr){
	$link = mysqli_connect($arr['DB_HOST'], $arr['DB_USER'], $arr['DB_PWD'], $arr['DB_NAME']);
	if (mysqli_errno($link)) {
		echo mysqli_error($link);
	}else{
		return $link;
	}
}



//操作数据库的添加
function add($arr, $link= null, $table=null){
	$keys = "";
	$values = "";
	foreach ($arr as $key => $value) {
		$keys .= $key.",";
		if (is_string($value)) {
			$value = "'".$value."'";
		}
		$values .= $value.",";
	}
	$keys = substr($keys, 0, strlen($keys)-1);
	$values = substr($values, 0, strlen($values)-1);
	$sql = "INSERT INTO {$table} ({$keys}) VALUES ({$values})";
	echo $sql;
	//设置编码格式
	mysqli_query($link, "set names utf8");
	$res = mysqli_query($link, $sql);
	//添加数据的id值
	$num = mysqli_insert_id($link);
	if($res && mysqli_affected_rows($link)) {
		return $num;
	}else{
		return false;
	}
}


//添加数据库的跟新操作
function upData($arr, $link=null, $table=null, $where=NULL){
 	$where = $where==NULL?"":" WHERE ".$where;
	$str ="";
	foreach ($arr as $key => $value) {
		if (is_string($value)) {
			$value = "'".$value."'";
		}
		$str .= $key."=".$value.",";
	}
	$str = substr($str, 0, strlen($str)-1);
	$sql = "UPDATE {$table} SET {$str}" .$where;
	mysqli_query($link, "set names utf8");
	$res = mysqli_query($link, $sql);
	if ($res && mysqli_affected_rows($link)) {
		return true;
	}else{
		return false;
	}
}


//删除数据
function delete(){
	$where = $where==null?"":" WHERE ".where;
	$sql = "DELETE FROM {$table}" .$where;
	$res = mysqli_query($link, $sql);
	if ($res && mysqli_affected_rows($link)) {
		return true;
	}else{
		return false;
	}
}

//获取多条数据
function getList($table, $link=null, $where=null){
	// $where = $where==null?"":" WHERE ".$where;
	$sql = "SELECT * FROM {$table}".$where;
	//设置编码格式
	mysqli_query($link, "set names utf8");
	$res = mysqli_query($link, $sql);
	while($data = mysqli_fetch_assoc($res)){
		$arr[] = $data;
	}
	if($res && mysqli_num_rows($res)){
		return $arr;
	}else{
		return false;
	}
}

//获取一条数据
function getOne($table,$link=null,$where=null){
	$where = $where==null?"":" WHERE ".$where;
	$sql = "SELECT * FROM {$table}".$where;
	mysqli_query($link, "set names utf8");
	$res = mysqli_query($link, $sql);
	$data = mysqli_fetch_assoc($res);
	if($res && mysqli_num_rows($res)){
		return $data;
	}else{
		return false;
	}
}


 ?>